news = new Object();

news.admin = new Object();

news.admin.initBody = function(id) {
	news.admin.initHelper();
	news.admin.requestAllSubMenu(function(menuList){
		var menu = {};
		var id = 0;
		var menuType = 0;
		var mainMenuId = 0;
		var menuOrderId = 0;
		var menuName = "";
		for(var i = 0 ; i < menuList.length ; i ++){
			menu = menuList[i];
			news.admin.appendSubmenu(menu);
		}
	});
//	if (id == 30) {
//		news.admin.initNewsList();
//		$("#main3").click();
//	} else if (id == 31) {
//		news.admin.initAddNews();
//		$("#main3").click();
//	} else if (id == 11) {
//		news.admin.initSubMenuManage(1, "概况介绍");
//		
//	}
}

news.admin.initHelper = function(){
	$("#news_body_right_modalPath span").text("使用帮助");
	$("#news_body_right_modalName span").text("使用帮助");
}

news.admin.appendSubmenu = function(menu){
	var id = menu.id;
	var menuType = menu.type;
	var mainMenuId = menu.parentMenuId;
	var menuOrderId = menu.orderId;
	var menuName = menu.menuName;
	var html = [];
	html.push('<li><a href="javascript:news.admin.initSubmenuContent(');
	html.push(''+id+','+mainMenuId+',\''+menuType);
	html.push('\');" menuType ="'+menuType);
	html.push('" mainMenuId = "' + mainMenuId);
	html.push('">- '+menuName);
	html.push('</a></li>');
	$("#child"+mainMenuId).find("ul").append(html.join(""));
}


news.admin.initSubmenuContent = function(id,mainMenuId,menuType){
	alert(id+","+mainMenuId+","+menuType);
}
/**
 * 新闻列表模块初始化
 */
news.admin.initNewsList = function() {
	$("#news_body_right_modalPath span").text("新闻信息 >> 新闻列表");
	$("#news_body_right_modalName span").text("新闻列表");
	news.admin.createNewsListHtml();
}

/**
 * 新闻列表模块初始化
 */
news.admin.createNewsListHtml = function() {

	news.admin.jQueryRequest({
		url : "docManage!findNewsListByPage.action",
		data : {
			pageNum : 0
		},
		showBlock : true,
		showText : '正在加载数据，请稍候...',
		cache : false,
		success : function(result) {
			var newsListInfo = result.newsList;
			var newsListHtml = news.admin.getNewsListHtml(newsListInfo);
			$("#news_body_right_modalContent").html(newsListHtml);
		},
		error : function() {
			alert("查询出错，请联系管理员！");
		},
		timeout : function() {
			alert("网络不畅通，提交超时,请稍后操作！");
		}
	});
}

/**
 * 获取新闻列表模块html
 */
news.admin.getNewsListHtml = function(newsListInfo) {
	var html = [ '<div style = "position:relative;left:20px;top:5px;font-size:13px;">' ];
	html.push('<table style = "font-size:13px;">');
	for (prop in newsListInfo) {
		html.push(news.admin.getSingleNewsHeadHtml(newsListInfo[prop]));
	}
	html.push('</table>');
	html.push('</div>');
	return html.join("");
}

news.admin.getSingleNewsHeadHtml = function(newsHead) {
	var id = newsHead[0];
	var newsTitle = newsHead[1];
	var submitTime = newsHead[2].replace("T", " ");
	var html = [];
	html.push('<tr height= 25 sign = ' + id + '>');
	html.push('<td width = 500>' + newsTitle);
	html.push('</td>');
	html.push('<td>[' + submitTime);
	html.push(']</td>');
	html.push('</tr>');
	return html.join("");
}
/**
 * 增加新闻模块初始化
 */
news.admin.initAddNews = function() {
	$("#news_body_right_modalPath span").text("新闻信息 >> 新闻增加");
	$("#news_body_right_modalName span").text("新闻增加");
	$("#news_body_right_modalContent").html(news.admin.getAddNewsHtml());
	UE.getEditor('myEditor');
}
/**
 * 获取增加新闻模块html
 */
news.admin.getAddNewsHtml = function() {
	var html = [];
	html
			.push('<form method="post" action="javascript:news.admin.addNewsSubmit();">');
	html
			.push('		<div  style = "width: 650px; height: 40px; padding-top: 10px; padding-left: 20px; font-size: 13px;"><span style="position: relative; top: -3px;">新闻标题：</span><input id = "newsTitle" type = "text" style="width: 550px;"/><span style="color:red;">*</span></div>');
	html
			.push('		<textarea id="myEditor" name="myeditor">这里写你的初始化内容</textarea>');
	html
			.push('		<div style="width:720px;height:40px;padding-top:10px;text-align:center;"><input style="border:#ff6600 1px solid;background-color:#ff6600;color:#ffffff;width:32px;text-align:center;height:20px;position:relative;" value="提交 " type="submit" name="submit"/></div>');
	html.push('</form>');
	return html.join("");
}
/**
 * 增加新闻模块提交按钮处理函数
 */
news.admin.addNewsSubmit = function() {
	var newsTitle = $("#newsTitle").val();
	var newsReporter = "admin";
	var newsContent = $("#myEditor").val();
	if ("" === newsTitle) {
		alert("标题不能为空！");
		return;
	}

	news.admin.jQueryRequest({
		url : "docManage.action",
		data : {
			newsTitle : newsTitle,
			newsReporter : newsReporter,
			newsContent : newsContent
		},
		showBlock : true,
		showText : '正在加载数据，请稍候...',
		cache : false,
		success : function(result) {
			news.admin.initNewsList();
		},
		error : function() {
			alert("提交出错，请联系管理员！");
		},
		timeout : function() {
			alert("网络不畅通，提交超时,请稍后操作！");
		}
	});
}

news.admin.request = function(options) {
	var url = null || options.url;
	var data = null || options.data;
	var successFun = function() {
	} || options.success;
	var errorFun = function() {
	} || options.error;
	var timeoutFun = function() {
	} || options.timeout;
	var type = "GET" || options.type;
	var async = true || options.async;
	if (url == null) {
		alert("url 不能为空！");
	}
	$.ajax({
		url : url,
		data : data,
		success : successFun,
		error : errorFun,
		timeout : timeoutFun,
		type : type,
		async : async
	});
}

/**
 * 子栏目管理
 */
news.admin.initSubMenuManage = function(id, menuName) {
	$("#main1").click(function(e){
		if(document.all.child1.style.display==='none'){
			news.admin.requestSubMenuInfo(id,function(menuList){
				news.admin.creatSubMenByMenuList(menuList);
			});
		}
		
	});
	$("#news_body_right_modalPath span").text("概况介绍 >> 子栏目设置");
	$("#news_body_right_modalName span").text("子栏目设置");
	news.admin.requestSubMenuInfo(id,function(menuList){
		news.admin.creatSubMenByMenuList(menuList);
		var newsListHtml = news.admin.getSubMenuManageHtml(menuList);
		news.admin.setModalContentHtml(newsListHtml);
		$("#main1").click();
	});
}

/**
 * 请求所有二级子栏目信息
 */
news.admin.requestAllSubMenu = function(completerHandler) {
	// news.admin.mask("正在查询子栏目，请稍候...");
	news.admin.jQueryRequest({
		url : "subMenuManage!findAllSubMenu.action",
		cache : false,
		success : function(result) {
			if(result.menuList!==undefined){
				completerHandler(result.menuList);
			}
		},
		error : function() {
			alert("查询出错，请联系管理员！");
		},
		timeoutHandler : function() {
			alert("网络不畅通，提交超时,请稍后操作！");
		}
	});

	// $.ajax();
}

/**
 * 请求子栏目信息
 */
news.admin.requestSubMenuInfo = function(id,completerHandler) {
	// news.admin.mask("正在查询子栏目，请稍候...");
	news.admin.jQueryRequest({
		url : "subMenuManage!findAllSubMenuById.action",
		data : {
			id : id
		},
		cache : false,
		success : function(result) {
			if(result.menuList!==undefined){
				completerHandler(result.menuList);
			}
		},
		error : function() {
			alert("查询出错，请联系管理员！");
		},
		timeoutHandler : function() {
			alert("网络不畅通，提交超时,请稍后操作！");
		}
	});

	// $.ajax();
}

/**
 * 子栏目管理
 */
news.admin.createSubMenuManageHtml = function(id) {
	// news.admin.mask("正在查询子栏目，请稍候...");
	news.admin.jQueryRequest({
		url : "subMenuManage!findAllSubMenuById.action",
		data : {
			id : id
		},
		showBlock : true,
		showText : '正在加载数据，请稍候...',
		cache : false,
		success : function(result) {
			var menuList = result.menuList;
			
		},
		error : function() {
			alert("查询出错，请联系管理员！");
		},
		timeoutHandler : function() {
			alert("网络不畅通，提交超时,请稍后操作！");
		}
	});

	// $.ajax();
}

news.admin.creatSubMenByMenuList = function(menuList){
	//<li><a href="jsp/admin.jsp?id=11">- 子栏目设置</a></li>
	var id = 0;
	var menuName = 0;
	var orderId = 0;
	var parentMenuId = 0;
	var type = 0;
	var menuInfo = {};
	var html = [];
	for(var i = 0 ; i < menuList.length ; i++){
		menuInfo = menuList[i];
		id = menuInfo.id;
		menuName = menuInfo.menuName;
		orderId = menuInfo.orderId;
		parentMenuId = menuInfo.parentMenuId;
		type = menuInfo.type;
		html.push('<li sign = "'+type+'"><a href="jsp/admin.jsp?id=' + id +'">- '+menuName+"</a></li>");
	}
	var mainMenuId = "#child"+parentMenuId;
	$(mainMenuId).find("ul").html(html.join(""));
}

news.admin.getSubMenuManageHtml = function(menuList) {
	var html = [ '<div>' ];
	html
			.push('<div style = "position:relative;left:20px;top:5px;font-size:13px;">');
	html
			.push('<table  style = "font-size:13px; border: #95C6F1 1px solid;"><tbpdy>');
	html.push('<tr height= 25 style = "background:#A6D0F6;">');
	html.push('<th width = 50 align = "center">');
	html.push('	 <input type = "checkbox" name = "subMenu_checkbox_all"/>');
	html.push('</th>');
	html.push('<th width = 250 align = "center">');
	html.push('	 <span>名称</span>');
	html.push('</th>');
	html.push('<th width = 150 align = "center">');
	html.push('	 <span>类型</span>');
	html.push('</th>');
	html.push('<th width = 100 align = "center">');
	html.push('	 <span>顺序</span>');
	html.push('</th>');
	html.push('<th width = 50 align = "center">');
	html.push('	 <span>操作</span>');
	html.push('</th>');
	html.push('</tr>');
	for (prop in menuList) {
		html.push(news.admin.getSingleSubMenuHtml(menuList[prop]));
	}
	html.push('</tbpdy></table>');
	html.push('</div>');
	html
			.push('<div style="width:700px;height:40px;padding-top:10px;text-align:center;">');
	html
			.push('		<input style="cursor:pointer;border:#ff6600 1px solid;background-color:#ff6600;color:#ffffff;width:32px;text-align:center;height:20px;position:relative;float:right;right:160px;" value="添加" type="submit" name="submit"/>');
	html
			.push('		<input style="cursor:pointer;border:#ff6600 1px solid;background-color:#ff6600;color:#ffffff;width:32px;text-align:center;height:20px;position:relative;float:right;right:80px;" value="删除" type="submit" name="submit"/>');
	html
			.push('		<input style="cursor:pointer;border:#ff6600 1px solid;background-color:#ff6600;color:#ffffff;width:32px;text-align:center;height:20px;position:relative;float:right;" value="排序 " type="submit" name="submit"/>');
	html.push('</div>');

	html.push('</div>');
	return html.join("");
}

news.admin.getSingleSubMenuHtml = function(menuInfo) {
	var id = menuInfo.id;
	var menuName = menuInfo.menuName;
	var orderId = menuInfo.orderId;
	var parentMenuId = menuInfo.parentMenuId;
	var type = menuInfo.type;
	if (type == 3) {
		return "";
	}
	var typeName = "独立页面";
	switch (type) {
	case 0:
		typeName = "独立页面";
		break;
	case 1:
		typeName = "滚动展示";
		break;
	case 2:
		typeName = "下载列表";
		break;
	case 3:
		typeName = "设置页面";
		break;
	}
	var html = [];
	html.push('<tr height= 25 sign = ' + id + ' parentMenuId=' + parentMenuId
			+ '>');
	html.push('<td width = 50 align = "center">');
	html.push('	 <input type = "checkbox" name = "subMenu_checkbox_' + id
			+ '"/>');
	html.push('</td>');
	html.push('<td width = 250 align = "center">');
	html.push('	 <span>' + menuName + '</span>');
	html.push('</td>');
	html.push('<td width = 150 align = "center">');
	html.push('	 <span>' + typeName + '</span>');
	html.push('</td>');
	html.push('<td width = 50 align = "center">');
	html.push('	 <input type = "text" name = subMenu_order_"' + id
			+ '" value="' + orderId
			+ '"  style="width:30px;position:relative;left:30px;"/>');
	html.push('</td>');
	html.push('<td width = 50 align = "center">');
	html
			.push('	 <a href="javascript:void(0);" title = "编辑"><img border="0" src="images/edit_g.gif"/></a>|');
	html
			.push('	 <a href="javascript:void(0);" title = "删除"><img border="0" src="images/delete_g.gif"/></a>');
	html.push('</td>');
	html.push('</tr>');
	return html.join("");
}

news.admin.setModalContentHtml = function(html) {
	$("#news_body_right_modalContent").html(html);
}

// 显示提示信息
function showBlockUI(blockParas) {
	// 提示信息
	var message = blockParas.message || "正在提交...";
	var showMessag = "<div style='font-size: 14px; font-weight: bold; margin-top: 8px;'>"
			+ blockParas.message + "</div>"

	// 提示信息样式
	var defaultCss = {
		width : '100px',
		top : '60%',
		left : '50%',
		height : '35px',
		color : '#e17009',
		border : '2px solid #79b7e7',
		backgroundColor : '#FAFCFC'
	};
	var cssStyle = blockParas.css || defaultCss;

	// 是否模态对话框
	var model = blockParas.model || true;

	jQuery.blockUI({
		message : showMessag,
		css : cssStyle,
		overlayCSS : {
			opacity : '0.0'
		},
		showOverlay : model
	});
}

/**
 * jQuery Ajax请求封装
 * 
 * @param {}
 *            paras
 * @return {}
 */
news.admin.jQueryRequest = function(paras) {
	var url = paras.url || "";
	var data = paras.data || "";
	var async = paras.async == false ? false : true;
	var dataType = paras.dataType || "json";
	var type = paras.type || "POST";
	var timeout = paras.timeout || 30000; // 超时时间，默认30秒
	var showBlock = paras.showBlock || false; // 是否显示提示模态信息，默认不显示
	var showText = paras.showText || "正在提交..."; // 提示信息内容
	var beforeRequest = paras.beforeRequest || null; // 请求前
	var completeRequest = paras.completeRequest || null; // 请求完成
	var sucessCallback = paras.success || null; // 请求成功返回
	var errorCallback = paras.error || null; // 请求返回失败
	var timeoutHandler = paras.timeoutHandler || null;// 请求超时
	var cache = paras.cache || false;
	// 请求前触发方法
	function beforeFun(request) {
		if (showBlock == true) {

			// 等待提示信息
			var showParas = {};
			showParas.message = showText;
			showBlockUI(showParas);

			// 显示提示信息
			function showBlockUI(blockParas) {
				// 提示信息
				var message = blockParas.message || "正在提交...";
				var width = ""+(message.length*14+10)+"px";
				var showMessag = "<div style='font-size: 14px; font-weight: bold; margin-top: 8px;'>"
						+ blockParas.message + "</div>"

				// 提示信息样式
				var defaultCss = {
					width : width,
					top : '60%',
					left : '50%',
					height : '35px',
					color : '#e17009',
					border : '2px solid #79b7e7',
					backgroundColor : '#FAFCFC'
				};
				var cssStyle = blockParas.css || defaultCss;

				// 是否模态对话框
				var model = blockParas.model || true;

				jQuery.blockUI({
					message : showMessag,
					css : cssStyle,
					overlayCSS : {
						opacity : '0.0'
					},
					showOverlay : model
				});
			}
		}

		// 调用请求前需要执行的方法
		if (beforeRequest != null) {
			beforeRequest(request);
		}
	}

	// 请求完成后触发方法
	function completeFun(request, status) {
		if (showBlock == true) {
			jQuery.unblockUI();
		}

		if (completeRequest != null) {
			completeRequest(request, status);
		}
	}

	// 请求出错触发方法
	function errorFun(request, status, exception) {
		// 超时触发方法
		if (status == "timeout") {
			// hik_base_tool_commontool.utils.alert("请求超时!");
			if (timeoutHandler != null) {
				timeoutHandler();
			}
			return;
		}
		// 错误处理方法
		if (errorCallback != null) {
			errorCallback(request, status);
		}
	}

	// 请求参数
	var ajaxParas = {
		type : type,
		url : url,
		async : async,
		timeout : timeout,
		cache : cache,
		data : data,
		dataType : dataType,
		beforeSend : beforeFun,
		complete : completeFun,
		error : errorFun
	};

	if (sucessCallback != null) {
		ajaxParas.success = sucessCallback;
	}

	return jQuery.ajax(ajaxParas);
}
